#include "schema.h"

#include "schemacomponent.h"

Schema::Schema()
{
    schema = NULL;
}

Schema::Schema(const Schema & other)
{
    if(other.schema != NULL){
        schema = new map<string, SchemaComponent *>();
        map<string,SchemaComponent *>::iterator it;
        for(it = schema->begin(); it != schema->end(); it++){
            schema->insert(pair<string,SchemaComponent *> ((*it).first, (*it).second->clone()));
        }//for
    }else schema = NULL;
}

Schema::~Schema()
{
    if(schema != NULL){
        map<string,SchemaComponent *>::iterator it;
        for(it = schema->begin(); it != schema->end(); it++){
            delete (*it).second;
        }//for
        delete schema; schema = NULL;
    }//if
}

string Schema::toString()
{
    string str = "SCHEMA: \n";
    if(schema != NULL){
        map<string, SchemaComponent *>::iterator it;
        for(it=schema->begin(); it != schema->end(); it++){
            str = str + (*it).second->toString() + "\n\n";
        }//for
    }// if
    return str;
}

bool Schema::get(const string & name, SchemaComponent * & component)
{
    if(schema == NULL){
        component = NULL;
        return false;
    }else{
        map<string, SchemaComponent *>::iterator it;
        it = schema->find(name);
        if(it == schema->end()){
            component = NULL;
            return false;
        }// if
        component = (*it).second;
        return true;
    }// if-else
}

void Schema::add(SchemaComponent * component)
{
    if(schema == NULL) schema = new map<string, SchemaComponent *>();
    if(component != NULL) schema->insert(pair<string, SchemaComponent *>(component->getName(), component->clone()));
}

Schema * Schema::clone()
{
    return new Schema(*this);
}

